package com.example.myapplication;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.widget.TextView;

import com.base.core.EmptyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.example.myapplication.R;

public class NoticeAdapter extends BaseQuickAdapter<NoticeBean, BaseViewHolder> {
    public NoticeAdapter() {
        super(null);
        setMultiTypeDelegate(new MultiTypeDelegate<NoticeBean>() {
            @Override
            protected int getItemType(NoticeBean entity) {
                return entity.getType();
            }
        });
        getMultiTypeDelegate()
                .registerItemType(0, R.layout.item_left)
                .registerItemType(1, R.layout.item_right)
                .registerItemType(3, R.layout.item_right)
                .registerItemType(2, R.layout.item_left);
    }

    @Override
    protected void convert(BaseViewHolder helper, NoticeBean item) {
        int type = item.getType();
        TextView tv_title = helper.getView(R.id.tv_title);
        TextView tv_time = helper.getView(R.id.tv_time);
        String time = TimeUtils.millis2String(item.getTime(), "yyyy年MM月dd日 HH:mm:ss");
        if (type == 0) {
            tv_title.setTextColor(Color.parseColor("#D00E0E"));
            tv_time.setTextColor(Color.parseColor("#D00E0E"));
            tv_title.setText("心跳数据发送成功");
            tv_time.setText("时间：" + time);
        } else if (type == 1) {
            tv_title.setTextColor(Color.parseColor("#101010"));
            tv_time.setTextColor(Color.parseColor("#101010"));
            tv_title.setText("收到通知信息：" + "标题：" + item.getTitle() + "。内容：" + item.getNoticeContent());
            tv_time.setText("时间：" + time + "\nApp名称:" + getAppName(item.getNotificationPkg()));
        } else if (type == 3) {
            tv_title.setTextColor(Color.parseColor("#101010"));
            tv_time.setTextColor(Color.parseColor("#101010"));
            tv_title.setText("收到短信信息：" + item.getNoticeContent());
            tv_time.setText("时间：" + time);
        } else if (type == 2) {
            tv_title.setTextColor(Color.parseColor("#101010"));
            tv_time.setTextColor(Color.parseColor("#101010"));
            tv_title.setText("信息发送：" + item.isSendStatus());
            tv_time.setText("时间：" + time);
        }
    }

    public String getAppName(String str) {
        try {
            PackageInfo info = null;
            PackageManager pm = mContext.getPackageManager();
            try {
                info = pm.getPackageInfo(str, PackageManager.GET_ACTIVITIES);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            String s = info.applicationInfo.loadLabel(pm).toString();
            return EmptyUtils.isNotEmpty(s) ? s : str;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
